package com.wlx.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.wlx.auth.domain.PortalUser;
import com.wlx.auth.service.PortalUserService;
import com.wlx.common.core.domain.R;
import com.wlx.common.core.web.controller.BaseController;
import com.wlx.common.mongodb.MongoDTO;
import com.wlx.common.mongodb.MongoDbResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 *
 * @author wlx
 */
@Api("注册用户信息（系统接口）")
@RestController
@RequestMapping("/userInner")
public class UserApiController extends BaseController {
    @Autowired
    private PortalUserService userService;

    /**
     * 请求样例
     * <p>
     * {
     * "current":1,
     * "size":10,
     * "sortParams":{
     * "registerTime":"desc"
     * },
     * "queryParams":[
     * {
     * "match":"=",
     * "key":"appId",
     * "value":"2cec228d27e6493fb8f26811540ce319"
     * },
     * {
     * "match":"in",
     * "key":"phone",
     * "valueList":["15123309443"]
     * },
     * {
     * "match":">=",
     * "key":"registerTime",
     * "value":1614762226790
     * },
     * {
     * "match":"<=",
     * "key":"registerTime",
     * "value":1614762226888
     * }
     * ]
     * }
     */

    @ApiOperation(value = "分页查询门户用户", notes = "分页查询门户用户")
    @GetMapping("/search/page")
    public R<?> searchPage(@RequestBody MongoDTO mongoDTO) {
        MongoDbResult mongoDbResult = userService.queryPageByParam(mongoDTO);
        return R.ok(mongoDbResult);
    }


    @ApiOperation(value = "查询指定门户用户", notes = "查询指定门户用户")
    @GetMapping("/detail/{userUuid}")
    public R<?> detail(@PathVariable("userUuid") String userUuid) {
        JSONObject jsonObject = userService.searchOne(userUuid);
        return R.ok(jsonObject);
    }

    @ApiOperation(value = "修改指定用户状态", notes = "修改指定用户状态")
    @PutMapping("/changeStatus")
    public R<?> update(@RequestBody PortalUser portalUser) {
        userService.updateOne(portalUser.getUuid(), portalUser.getStatus());
        return R.ok();
    }


    @ApiOperation(value = "删除指定门户用户", notes = "删除指定门户用户")
    @DeleteMapping("/delete/{userUuid}")
    public R<?> delete(@PathVariable("userUuid") String userUuid) {
        userService.deleteOne(userUuid);
        return R.ok();
    }

    @ApiOperation(value = "聚合查询", notes = "聚合查询")
    @GetMapping("/queryAggregate/{userUuid}")
    public R<?> queryAggregate(@PathVariable("userUuid") String userUuid) {
        List<JSONObject> jsonObjects = userService.queryAggregate(userUuid);
        return R.ok(jsonObjects);
    }

}
